package com.ruyuan.eshop.common.dao.impl;

import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruyuan.eshop.common.dao.OrderDAO;
import com.ruyuan.eshop.common.domain.entity.OrderDO;
import com.ruyuan.eshop.common.domain.vo.OrderSkuVO;
import com.ruyuan.eshop.common.enums.*;
import com.ruyuan.eshop.common.mapper.OrderMapper;
import com.ruyuan.eshop.framework.utils.DateUtil;
import com.ruyuan.eshop.framework.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 订单DAO
 * @author pwd
 * @version 1.0
 */
@Repository
public class OrderDAOImpl extends ServiceImpl<OrderMapper, OrderDO> implements OrderDAO {

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public void updateServiceStatus(String orderSn, OrderServiceStatusEnum serviceStatus) {
        new UpdateChainWrapper<>(orderMapper)
                //设置售后状态
                .set("service_status", serviceStatus.value())
                //按订单编号修改
                .eq("sn", orderSn)
                //提交修改
                .update();
    }

    @Override
    public void updateOrderPrice(Double orderPrice, Double orderDiscoutPrice, List<OrderSkuVO> list, String orderSn) {
        //修改订单价格
        new UpdateChainWrapper<>(orderMapper)
                //设置订单价格
                .set("order_price", orderPrice)
                //设置应付金额
                .set("need_pay_money", orderPrice)
                //设置优惠金额
                .set("discount_price", orderDiscoutPrice)
                //设置货物列表json
                .set("items_json", JsonUtil.objectToJson(list))
                //按订单编号修改
                .eq("sn", orderSn)
                //提交修改
                .update();
    }

    @Override
    public void updateCommentStatus(String orderSn, CommentStatusEnum commentStatus) {
        new UpdateChainWrapper<>(orderMapper)
                //设置评论是否完成
                .set("comment_status", commentStatus.name())
                //按订单标号修改
                .eq("sn", orderSn)
                //提交修改
                .update();
    }

    @Override
    public void updateItemJson(String itemsJson, String orderSn) {
        new UpdateChainWrapper<>(orderMapper)
                .set("items_json", itemsJson)
                //按订单编号修改
                .eq("sn", orderSn)
                //提交修改
                .update();
    }

    @Override
    public void updateOrderStatus(String orderSn, OrderStatusEnum orderStatus) {
        new UpdateChainWrapper<>(orderMapper)
                //设置订单状态
                .set("order_status", orderStatus.value())
                //如果订单状态是已付款状态，设置付款状态为已付款
                .set(OrderStatusEnum.PAID_OFF.equals(orderStatus), "pay_status", PayStatusEnum.PAY_YES.value())
                //设置支付时间
                .set(OrderStatusEnum.PAID_OFF.equals(orderStatus), "payment_time", DateUtil.getDateline())
                //按订单编号修改
                .eq("sn", orderSn)
                //提交修改
                .update();
    }

    @Override
    public void updatePaymentInfo(String orderSn, String methodName) {
        // 后台点击确认收款，清空支付方式
        new UpdateChainWrapper<>(orderMapper)
                //设置支付插件id为null
                .set("payment_plugin_id", null)
                //设置支付插件名称为methodName
                .set("payment_method_name", methodName)
                //按订单编号修改
                .eq("sn", orderSn)
                //提交修改
                .update();
    }

    @Override
    public void updateOrderPriceAndTime(String orderSn, long nowTime, Double payPrice) {
        new UpdateChainWrapper<>(orderMapper)
                //设置订单状态为已付款
                .set("order_status", OrderStatusEnum.PAID_OFF.value())
                //设置支付状态为已付款
                .set("pay_status", PayStatusEnum.PAY_YES.value())
                //设置发货状态为已发货
                .set("ship_status", ShipStatusEnum.SHIP_ROG.value())
                //设置已支付金额
                .set("pay_money", payPrice)
                //设置支付时间
                .set("payment_time", nowTime)
                //按订单编号修改
                .eq("sn", orderSn)
                //提交修改
                .update();
    }

    @Override
    public void updateOrderPriceAndTimeAndPayNo(String orderSn, long nowTime, Double payPrice, String returnTradeNo) {
        //如果是在线支付
        new UpdateChainWrapper<>(orderMapper)
                //设置订单状态为已付款
                .set("order_status", OrderStatusEnum.PAID_OFF.value())
                //设置支付状态为已付款
                .set("pay_status", PayStatusEnum.PAY_YES.value())
                //设置已支付金额
                .set("pay_money", payPrice)
                //设置支付时间
                .set("payment_time", nowTime)
                //设置支付方式返回的交易号
                .set("pay_order_no", returnTradeNo)
                //按订单编号修改
                .eq("sn", orderSn)
                //提交修改
                .update();
    }

    @Override
    public void updateOrderLogic(String orderSn, String logiName, Long logiId, long nowTime, String deliveryNo) {
        new UpdateChainWrapper<>(orderMapper)
                //设置订单状态为已发货
                .set("order_status", OrderStatusEnum.SHIPPED.value())
                //设置发货状态为已发货
                .set("ship_status", ShipStatusEnum.SHIP_YES.value())
                //设置货运单号
                .set("ship_no", deliveryNo)
                //设置送货时间
                .set("ship_time", nowTime)
                //设置物流公司
                .set("logi_id", logiId)
                //设置物流公司名称
                .set("logi_name", logiName)
                //按订单编号修改
                .eq("sn", orderSn)
                //提交修改
                .update();
    }

    @Override
    public void editOrderShopName(Long shopId, String shopName) {
        new UpdateChainWrapper<>(orderMapper)
                //设置商家名称
                .set("seller_name", shopName)
                //按商家id修改
                .eq("seller_id", shopId)
                //提交修改
                .update();
    }

    @Override
    public void updateOrderPayTime(String orderSn, long nowTime) {
        new UpdateChainWrapper<>(orderMapper)
                //设置订单状态为已完成
                .set("order_status", OrderStatusEnum.COMPLETE.value())
                //设置完成时间
                .set("complete_time", nowTime)
                //按订单编号修改
                .eq("sn", orderSn)
                //提交修改
                .update();
    }
}
